#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
    {
        ListNode* PHead = new ListNode(-1);
        ListNode* cur = PHead;
        int Next = 0;
        while (l1 != nullptr || l2 != nullptr)
        {
            int value = Next;
            if (l1 != nullptr)
            {
                value += l1->val;
                l1 = l1->next;
            }
            if (l2 != nullptr)
            {
                value += l2->val;
                l2 = l2->next;
            }
            Next = value / 10;
            value = value % 10;
            cur->next = new ListNode(value);
            cur = cur->next;
        }
        if (Next)
            cur->next = new ListNode(Next);
        cur = PHead;
        PHead = PHead->next;
        delete cur;
        return PHead;
    }
};

